/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pe.gov.mtc.dgac.licencias.action;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.DispatchAction;
import pe.gov.mtc.dgac.licencias.bean.ClienteTO;
import pe.gov.mtc.dgac.licencias.bean.ClientelicenciaTO;
import pe.gov.mtc.dgac.licencias.bean.LicenciaTO;
import pe.gov.mtc.dgac.licencias.service.ClientelicenciaService;
import pe.gov.mtc.dgac.licencias.util.Page;

/**
 *
 * @author German
 */
public class ClientelicenciaAction extends DispatchAction {
   private Log logger = LogFactory.getLog(this.getClass());
    private ClientelicenciaService clilicService;
    
    
    public ClientelicenciaAction(ClientelicenciaService clilicService) {
        super();
        this.clilicService = clilicService;
                
    }  
   
    public ActionForward buscarClientelicencia(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse response) throws Exception {
        logger.debug("buscarClientelicencia");
        String forward="success";
        ActionMessages errors = new ActionMessages(); 
        String metodo = req.getParameter("metodo");
        String numlic = req.getParameter("txtnumlic");
        String codlic = req.getParameter("txttipolic");
        String fecotor = req.getParameter("txtfecotor");
        String clinom = req.getParameter("txtnombre");
        String cliapepat = req.getParameter("txtapepat");
        String cliapemat = req.getParameter("txtapemat");
        String busqueda=req.getParameter("busqueda");
        String page=req.getParameter("page");
        String pageSize=req.getParameter("pageSize");
        try {
            HttpSession sesion = req.getSession();
            boolean nuevabusqueda=false;
            if(metodo.equals("buscar")){
              //List<ClientelicenciaTO> listadoClienteLicencias;  
             
             // ClienteLicenciaManager climgr = new ClienteLicenciaManager();
              if(page==null)page="";
              if(page.equals("")) page="1";
              if(page.equals("0"))nuevabusqueda=true;
              int pagina=Integer.parseInt(page);
              
              if(nuevabusqueda) {
                ClientelicenciaTO cli_to = new ClientelicenciaTO();
                ClienteTO cli=new ClienteTO();
                LicenciaTO lic=new LicenciaTO();
                if (numlic!=null) cli_to.setNumLic(numlic);
                if (fecotor!=null) cli_to.setSfechaOtorgamiento(fecotor);
                if (cliapepat!=null) cli.setApellidoPat(cliapepat);
                if (cliapemat!=null) cli.setApellidoMat(cliapemat);
                if (clinom!=null) cli.setNombres(clinom);
                if (codlic!=null) lic.setCodLicencia(Long.parseLong(codlic));
              
                cli_to.setClienteTO(cli);
                cli_to.setLicenciaTO(lic);
                Page pageClienteLicencias=clilicService.buscarClientelicencia(cli_to,pagina,Page.SIZE_PAGE);
              //listadoClienteLicencias = pageClienteLicencias.getResults();
                sesion.setAttribute("listClienteLicencias", pageClienteLicencias);
                sesion.setAttribute("cli_to", cli_to);
              }else{
                ClientelicenciaTO cli_to=(ClientelicenciaTO)sesion.getAttribute("cli_to");
                Page pageClienteLicencias=clilicService.buscarClientelicencia(cli_to,pagina,Page.SIZE_PAGE);
              //listadoClienteLicencias = pageClienteLicencias.getResults();
                sesion.setAttribute("listClienteLicencias", pageClienteLicencias);  
              }
            }
            
            if(busqueda!=null){
              //getServletContext().getRequestDispatcher("/licencias/LicenciasFormList.jsp?busqueda=1").forward(req, response);   
              forward="busqueda";  
            }else{
              //getServletContext().getRequestDispatcher("/licencias/LicenciasFormList.jsp").forward(req, response);
              forward="success";
            }
                  
    }catch(Exception e) {
           logger.error(e);
           errors.add("login", new ActionMessage("errors.interno","buscarClientelicencia"));  
    }
              
        return mapping.findForward(forward);
    }   
  
   public ActionForward buscarClientelicenciaXLS(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse response) throws Exception {
        logger.debug("buscarClientelicenciaXLS");
        
        HttpSession sesion = req.getSession();
          
        Page result=(Page) req.getSession().getAttribute("listClienteLicencias");
        result.setPageSize(result.getTotalElements());
        sesion.setAttribute("listClienteLicencias",result);
              
        return mapping.findForward("xls");
        
    }
}
